package edu.suda.xsap.service.impl;
import edu.suda.xsap.dto.MemberDto;
import edu.suda.xsap.dto.convert.MemberConvert;
import edu.suda.xsap.entity.Member;
import edu.suda.xsap.mapper.MemberMapper;
import edu.suda.xsap.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@Service
public class MemberServiceImpl implements MemberService{
    @Autowired
    private MemberConvert memberConvert;//定义转换器

    @Autowired
    private MemberMapper memberMapper;//定义持久层接口

    @Override
    public void add(Member member) {
        memberMapper.add(member);
    }//保存


    @Override
    public void update(Member member) { memberMapper.update(member);}//更新

    @Override
    public void deleteById(Integer id) {
        memberMapper.deleteById(id);
    }//删除通过id
    @Override
    public void deleteSelected(Integer[] ids){//删除多个
        if(ids != null) {
            Arrays.stream(ids).forEach(System.out::println);
        }
        memberMapper.deleteSelected(ids);
    }



    @Override
    public List<MemberDto> findByName(String name) {//根据姓名查询
        List<Member> members = memberMapper.findByName(name);
        return memberConvert.toDtoList(members);
    }

    @Override
    public List<MemberDto> findById(Integer id) {//根据姓名查询
        List<Member> members = memberMapper.findById(id);
        return memberConvert.toDtoList(members);
    }

    @Override
    public List<MemberDto> findAll() {//查询所有
        //
        List<Member> members = memberMapper.findAll();
//        System.out.println(members);
        //要把这个List<Log>转换成List<LogDto>
        return memberConvert.toDtoList(members);
    }
}


